<script lang="ts" setup>
import { ref, h, reactive, onMounted,onBeforeMount } from "vue";
import { ReVxeGrid } from "@/components/ReVxeTable";
import { VxeGridPropTypes, VxeUI } from "vxe-table";
import { VxeFormProps, VxeButton,VxeCheckbox,VxeImage } from "vxe-pc-ui";
import { ElMessage } from "element-plus";
import { finalstatementList } from "@/api/business/business";
import {
  MerchantsInfoByTaxSourcesId,
} from "@/api/welcome";
const reVxeGridRef = ref();
let taxSourcesId = Number(sessionStorage.getItem("taxSourcesId"))
onMounted(() => {
  // MerchantsInfoByTaxSourcesId(taxSourcesId);
  getMerchantsInfoByTaxSourcesId( taxSourcesId )
});
interface RestaurantItem {
  id: number
  value: string
}
const listMerchantsInfoByTaxSourcesId = ref<RestaurantItem[]>([]);
const getMerchantsInfoByTaxSourcesId = (taxSourcesId?:
  number): void => {
  MerchantsInfoByTaxSourcesId(taxSourcesId).then((res: any) => {
    listMerchantsInfoByTaxSourcesId.value = res.data;
  });
};
// 根据税源地ID查询合作公司列表搜索
const fetchSuggestions = (queryString: any, cb: (params: any) => void) => { 
  const results = queryString
    ? listMerchantsInfoByTaxSourcesId.value.filter(createFilter(queryString))
    : listMerchantsInfoByTaxSourcesId.value  
    cb(results)
  
};
const createFilter = (queryString: string) => {
  return (listMerchantsInfoByTaxSourcesId: RestaurantItem) => {
    return (
      listMerchantsInfoByTaxSourcesId.value.indexOf(queryString) === 0
    )
  }
}
const autoselect = (res: any) => {
  console.log(res);
  formOptions.data.merchantsName = res.value;
};
const columns: VxeGridPropTypes.Columns<any> = [
  {
    title: "合作公司",
    field: "merchantsInfoIdDisplay",
    align: "center",
    minWidth: 100,
    showOverflow: true
  },
  {
    title: "结算月份",
    field: "finalStatementMonth",
    align: "center",
    minWidth: 100,    
  },   
  {
    title: "操作",
    field: "operate",
    align: "center",
    minWidth: 100,
    slots: {
      default: ({ row }) => {
        let list = [h(VxeButton, {
              status: "primary",
              mode: "text",
              size: "mini",
              content: "查看",
              onClick() {
                if(!row.finalStatementUrl){
                  ElMessage.error("查看地址不存在！");
                  return false;
                }
                window.open(row.finalStatementUrl, "_blank");
              }
            })]
            return list;
      }
    }
  }
   
];
// 筛选相关
const formRef = ref();
interface FormDataVO {
  merchantsName: string;//公司名称
  taxSourcesId: number | string; // 税源地 
  finalStatementMonth: string
  
}
const formOptions = reactive<VxeFormProps<FormDataVO>>({
  data: {
    merchantsName: '',
    taxSourcesId: taxSourcesId, 
    finalStatementMonth:''   
  },
  items: [
    {
      field: "merchantsName",
      title: "",
      span: 4,
      itemRender: {},
      slots: { default: "merchantsName" }
    },  
    {
      field: "finalStatementMonth",
      title: "",
      span: 4,
      itemRender: {},
      slots: { default: "finalStatementMonth" }
    },   
    { align: "left", span: 4, slots: { default: "active" } }
  ]
});
// 重置内容
const handleInitialFormParams = () => {
  formOptions.data = <FormDataVO>{
    taxSourcesId: '',
    merchantsName: '',
    finalStatementMonth: ""
  };
};
const handleSearch = () => {
  reVxeGridRef.value.loadData();
};
const handleReset = async () => {
  await handleInitialFormParams();
  reVxeGridRef.value.loadData("reset");
};

const handleView = (record: Recordable) => {
  
};
const functions: Record<string, string> = {
  add: "platform.client.index.add",
  edit: "platform.client.index.edit",
  view: "platform.client.index.view",
  delete: "platform.client.index.delete"
};


</script>
<template>
  <div>
    <el-card :shadow="`never`">
      <vxe-form ref="formRef" v-bind="formOptions">
        <template #merchantsName="{ data }">
          <el-autocomplete
            clearable
            v-model="data.merchantsName"
            :fetch-suggestions="fetchSuggestions"
            placeholder="请输入公司名称"
            value-key="value"
            @select="autoselect"
          >
          </el-autocomplete>
        </template> 
        <template #finalStatementMonth="{ data }">
          <el-date-picker
            clearable
            style="width: 100%"
            v-model="data.finalStatementMonth"
            type="month"
            value-format="YYYY-MM"
            placeholder="选择月"
          />
        </template>       
        <template #active>
          <el-button @click="handleSearch" native-type="submit" type="primary"
            >搜索</el-button
          >
          <el-button @click="handleReset" native-type="reset">重置</el-button>          
        </template>
      </vxe-form>
    </el-card>
    <el-card :shadow="`never`" class="table-card">
      <ReVxeGrid
        ref="reVxeGridRef"
        :request="finalstatementList"
        :functions="functions"
        :searchParams="formOptions.data"
        :columns="columns"
        :action="false"
        @handleView="handleView"
      />
    </el-card>
  </div>
</template>
<style>
</style>
